package com.canaan.business.service.agv.impl;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import com.canaan.business.domain.agv.AgvTaskConfig;
import com.canaan.business.domain.agv.UpdateAgvTaskConfigParam;
import com.canaan.business.mapper.agv.AgvTaskConfigMapper;
import com.canaan.business.service.agv.IAgvTaskConfigService;
import org.springframework.stereotype.Service;
import com.canaan.common.exception.ServiceException;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.bean.BeanUtils;

import cn.hutool.core.collection.CollUtil;

/**
 * 任务配置Service业务层处理
 *
 * @author
 * @date 2023-03-02
 */
@Service
public class AgvTaskConfigServiceImpl implements IAgvTaskConfigService {
    @Resource
    private AgvTaskConfigMapper agvTaskConfigMapper;

    /**
     * 查询任务配置
     *
     * @param id 任务配置主键
     * @return 任务配置
     */
    @Override
    public AgvTaskConfig selectAgvTaskConfigById(Long id) {
        return agvTaskConfigMapper.selectAgvTaskConfigById(id);
    }

    /**
     * 查询任务配置列表
     *
     * @param agvTaskConfig 任务配置
     * @return 任务配置
     */
    @Override
    public List<AgvTaskConfig> selectAgvTaskConfigList(AgvTaskConfig agvTaskConfig) {
        return agvTaskConfigMapper.selectAgvTaskConfigList(agvTaskConfig);
    }

    /**
     * 新增任务配置
     *
     * @param agvTaskConfig 任务配置
     * @return 结果
     */
    @Override
    public int insertAgvTaskConfig(AgvTaskConfig agvTaskConfig) {
        agvTaskConfig.setCreateTime(DateUtils.getNowDate());
        return agvTaskConfigMapper.insertAgvTaskConfig(agvTaskConfig);
    }

    /**
     * 批量新增任务配置
     *
     * @param agvTaskConfigList 任务配置列表
     * @return 结果
     */
    @Override
    public int batchInsertAgvTaskConfig(List<AgvTaskConfig> agvTaskConfigList) {
        Date currerTime = DateUtils.getNowDate();
        for (AgvTaskConfig agvTaskConfig : agvTaskConfigList){
            agvTaskConfig.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<agvTaskConfigList.size();) {
            int endIndex = i+500;
            if (endIndex > agvTaskConfigList.size()) {
                endIndex = agvTaskConfigList.size();
            }
            insertNum = insertNum + agvTaskConfigMapper.batchInsertAgvTaskConfig(agvTaskConfigList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改任务配置
     *
     * @param agvTaskConfig 任务配置
     * @return 结果
     */
    @Override
    public int updateAgvTaskConfig(AgvTaskConfig agvTaskConfig) {
        agvTaskConfig.setUpdateTime(DateUtils.getNowDate());
        return agvTaskConfigMapper.updateAgvTaskConfig(agvTaskConfig);
    }

    /**
     * 批量修改任务配置
     *
     * @param agvTaskConfigList 任务配置列表
     * @return 结果
     */
    @Override
    public int batchUpdateAgvTaskConfig(List<AgvTaskConfig> agvTaskConfigList) {
        Date currerTime = DateUtils.getNowDate();
        for (AgvTaskConfig agvTaskConfig : agvTaskConfigList){
            agvTaskConfig.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<agvTaskConfigList.size();) {
            int endIndex = i+500;
            if (endIndex > agvTaskConfigList.size()) {
                endIndex = agvTaskConfigList.size();
            }
            updateNum = updateNum + agvTaskConfigMapper.batchUpdateAgvTaskConfig(agvTaskConfigList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除任务配置
     *
     * @param ids 需要删除的任务配置主键集合
     * @return 结果
     */
    @Override
    public int deleteAgvTaskConfigByIds(Long[] ids) {
        return agvTaskConfigMapper.deleteAgvTaskConfigByIds(ids);
    }

    /**
     * 删除任务配置信息
     *
     * @param id 任务配置主键
     * @return 结果
     */
    @Override
    public int deleteAgvTaskConfigById(Long id) {
        return agvTaskConfigMapper.deleteAgvTaskConfigById(id);
    }

    /**
     * 更新
     *
     * @param param 更新参数对象
     */
    @Override
    public void updateAgvTaskConfig(UpdateAgvTaskConfigParam param) {
        if (Objects.isNull(param)) {
            throw new ServiceException("参数异常!");
        }
        AgvTaskConfig agvTaskConfig = this.selectAgvTaskConfigById(param.getId());
        if (Objects.isNull(agvTaskConfig)) {
            throw new ServiceException("根据id:" + param.getId() + " 没有查询到任务配置信息!");
        }
        BeanUtils.copyProperties(param, agvTaskConfig);
        this.updateAgvTaskConfig(agvTaskConfig);
    }

    /**
     * 根据源库位编码查询
     *
     * @param startAreaCode 源库区编码
     * @return
     */
    @Override
    public AgvTaskConfig getByStartAreaCode(String startAreaCode) {
        AgvTaskConfig queryParam = new AgvTaskConfig();
        queryParam.setStartAreaCode(startAreaCode);
        List<AgvTaskConfig> list = this.agvTaskConfigMapper.selectAgvTaskConfigList(queryParam);
        if (CollUtil.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }
}
